package com.wangzhenyu.learn.test.android_.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wangzhenyu.common.base.BaseVBActivity
import com.wangzhenyu.learn.R
import com.wangzhenyu.learn.databinding.ActivityTestBinding
import com.wangzhenyu.learn.test.android_.activityViewModel.*

class TestActivity : BaseVBActivity<ActivityTestBinding>() {


    override fun getViewBinding() = ActivityTestBinding.inflate(layoutInflater)

    private val viewModel: TestViewModel by viewModels()


    companion object {
        //UI测试
        private const val TYPE_ONE = 1

        //日常测试
        private const val TYPE_TWO = 2
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //RecyclerView
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = TestAdapter(viewModel.testData.parent)

    }


    //这是一个测试项目列表
    private inner class TestAdapter(private val data: MutableList<TestItemType>) :
        RecyclerView.Adapter<RecyclerView.ViewHolder>() {

        //父类Item
        private inner class TestParentViewHolder(view: View) : RecyclerView.ViewHolder(view),
            View.OnClickListener {

            private var testItem: TestItem.TestParentItem? = null

            private val textView: TextView = itemView.findViewById(R.id.tv)

            fun bind(item: TestItem.TestParentItem) {
                this.testItem = item
                textView.text = item.name
            }

            init {
                itemView.setOnClickListener(this)
            }

            override fun onClick(v: View?) {
                when (testItem?.isExpand) {
                    true -> {
                        closeChildItem(layoutPosition)
                        (data[layoutPosition] as TestItem.TestParentItem).isExpand =
                            !(data[layoutPosition] as TestItem.TestParentItem).isExpand

                    }
                    false -> {
                        openChildItem(layoutPosition)
                        (data[layoutPosition] as TestItem.TestParentItem).isExpand =
                            !(data[layoutPosition] as TestItem.TestParentItem).isExpand
                    }
                    else -> {
                    }
                }
            }
        }

        //子类Item
        private inner class TestChildViewHolder(view: View) : RecyclerView.ViewHolder(view),
            View.OnClickListener {

            private var testItem: TestItem.TestChildItem? = null
            private val textView: TextView = itemView.findViewById(R.id.tv)

            fun bind(item: TestItem.TestChildItem) {
                this.testItem = item
                textView.text = item.name
            }

            init {
                itemView.setOnClickListener(this)
            }

            override fun onClick(v: View?) {
                when (testItem?.name) {
                    TestSelect.list1[0] -> {
                        TestSelect.firstZero(this@TestActivity)
                    }
                    TestSelect.list2[0] -> {
                        TestSelect.secondZero(this@TestActivity)
                    }
                    TestSelect.list2[1] -> {
                        TestSelect.secondOne(this@TestActivity)
                    }
                }
            }
        }

        override fun getItemViewType(position: Int): Int {
            return if (data[position].getTestItemType() == TestItemEnum.TYPE_PARENT) TYPE_ONE else TYPE_TWO
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return if (viewType == TYPE_ONE) TestParentViewHolder(
                LayoutInflater.from(parent.context).inflate(
                    R.layout.test_item_test01, parent, false
                )
            )
            else TestChildViewHolder(
                LayoutInflater.from(parent.context).inflate(
                    R.layout.test_item_test02, parent, false
                )
            )
        }

        override fun getItemCount() = data.size

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (holder is TestParentViewHolder) {
                holder.bind(data[position] as TestItem.TestParentItem)

            } else if (holder is TestChildViewHolder) {
                holder.bind(data[position] as TestItem.TestChildItem)
            }
        }

        private fun openChildItem(position: Int) {
            val temp = (data[position] as TestItem.TestParentItem).child
            data.addAll(position + 1, temp)
            notifyItemRangeInserted(position + 1, temp.size)
        }

        private fun closeChildItem(position: Int) {
            val temp = (data[position] as TestItem.TestParentItem).child
            data.removeAll(temp)
            notifyItemRangeRemoved(position + 1, temp.size)
        }
    }
}